package cn.changeforyou.analyzer.javaparserDemo;

import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier.Keyword;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.expr.AssignExpr;
import com.github.javaparser.ast.expr.FieldAccessExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.ThisExpr;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.utils.SourceRoot;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;

/**
 * https://www.javacodegeeks.com/2017/12/javaparser-generate-analyze-modify-java-code.html
 * @author zhyu
 * @date 2021/2/3 12:44
 */
public class Csdn_dnc8371Demo {

    @SneakyThrows
    @Test
    public void test(){
        // Parse all source files
//        SourceRoot sourceRoot = new SourceRoot(Paths.get("D:\\code\\analyzer\\analyzer\\src\\main\\resources\\javaFiles"));
//        ParserConfiguration parserConfiguration = new ParserConfiguration();
//        sourceRoot.setParserConfiguration(parserConfiguration);
//        List<ParseResult> parseResults = sourceRoot.tryToParse("");
//
//// Now get all compilation unitsList 
//        allCus = parseResults.stream()
//            .filter(ParseResult::isSuccessful)
//            .map(r -> r.getResult().get())
//            .collect(Collectors.toList());
    }
    
    @Test
    public void test_writeCode() {
        CompilationUnit cu = new CompilationUnit();

        cu.setPackageDeclaration("jpexample.model");

        ClassOrInterfaceDeclaration book = cu.addClass("Book");
        book.addField("String", "title");
        book.addField("Person", "author");

        book.addConstructor(Keyword.PUBLIC)
            .addParameter("String", "title")
            .addParameter("Person", "author")
            .setBody(new BlockStmt()
                .addStatement(new ExpressionStmt(new AssignExpr(
                    new FieldAccessExpr(new ThisExpr(), "title"),
                    new NameExpr("title"),
                    AssignExpr.Operator.ASSIGN)))
                .addStatement(new ExpressionStmt(new AssignExpr(
                    new FieldAccessExpr(new ThisExpr(), "author"),
                    new NameExpr("author"),
                    AssignExpr.Operator.ASSIGN))));

        book.addMethod("getTitle", Keyword.PUBLIC).setBody(
            new BlockStmt().addStatement(new ReturnStmt(new NameExpr("title"))));

        book.addMethod("getAuthor", Keyword.PUBLIC).setBody(
            new BlockStmt().addStatement(new ReturnStmt(new NameExpr("author"))));

        System.out.println(cu.toString());
    }
}
